package com.wantong.jianpu.net;



import com.wantong.jianpu.base.BaseResultBean;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import me.goldze.mvvmhabit.utils.KLog;
import me.goldze.mvvmhabit.utils.ToastUtils;

/**
 * @author PACY
 * 创建时间 19/1/10 16:30.
 * 邮箱 pengwwe457621023@qq.com
 * 类描述 统一处理请求结果
 */
public abstract class BaseObserver<T> implements Observer<BaseResultBean<T>> {
    @Override
    public void onSubscribe(Disposable d) {

    }

    @Override
    public void onComplete() {

    }


    @Override
    public void onError(Throwable e) {
        KLog.e(e.toString());
        onError("请求失败,请重试");
        onComplete();
    }

    @Override
    public void onNext(BaseResultBean<T> t) {
        if (t.isSuccess()) {
            onSuccess(t);
        } else {
            onError(t.getMsg());
        }
        onComplete();

    }

    //具体实现下面两个方法，便可从中得到更直接详细的信息
    public abstract void onSuccess(BaseResultBean<T> t);

    public void onError(String errMessage) {
        ToastUtils.show(errMessage);
    };
}
